package com.lawfinal.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lawfinal.entity.Page;
import com.lawfinal.service.PageService;
import com.lawfinal.utils.Result;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 页面 前端控制器
 * </p>
 *
 * @author KBr
 * @since 2024-04-06
 */
@RestController
@CrossOrigin("http://localhost:5173/")
@MapperScan("com.lawfinal.mapper")
@RequestMapping("/page")
public class PageController {

      @Autowired
      private PageService pageService;

      @RequestMapping("/PageTop")
      public Result pageTop(Page page){
            LambdaQueryWrapper<Page> lambdaQueryWrapper = new LambdaQueryWrapper<>();

            if (StringUtils.isNotBlank(page.getDetail())){
                  lambdaQueryWrapper.like(Page::getDetail,page.getDetail());
            }

            return Result.sucess(pageService.list(lambdaQueryWrapper));

      }
      @RequestMapping("/list")
      public List<Page> list(){
            return pageService.list();
      }

      @RequestMapping("/add")
      public Result save(@RequestBody Page page){
            return pageService.save(page)?Result.sucess():Result.fail();
      }
      @RequestMapping("/toPageByID")
      public Result toPageByID( Page page){
            List list=  pageService.lambdaQuery().eq(Page::getId,page.getId()).list();
            return list.size()>0?Result.sucess(list.get(0)):Result.fail();
      }

      @RequestMapping("/del")
      public Result deleteById(String id){
            return pageService.removeById(id)?Result.sucess():Result.fail();
      }

      @RequestMapping("/update")
      public Result update(@RequestBody Page page){
            return  pageService.updateById(page)?Result.sucess():Result.fail();
      }
}
